package org.spiderflow.core.job;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spiderflow.core.Spider;
import org.spiderflow.core.model.SpiderFlow;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 爬虫定时执行管理
 *
 * @author Administrator
 */
@Component
public class SpiderJobManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpiderJobManager.class);

    private static final String JOB_NAME = "SPIDER_TASK_";

    public static final String JOB_PARAM_NAME = "SPIDER_FLOW";

    private final SpiderJob spiderJob;

    /**
     * 调度器
     */
    private final Scheduler scheduler;

    public SpiderJobManager(SpiderJob spiderJob, Scheduler scheduler) {
        this.spiderJob = spiderJob;
        this.scheduler = scheduler;
    }

    private JobKey getJobKey(String id) {
        return JobKey.jobKey(JOB_NAME + id);
    }

    private TriggerKey getTriggerKey(String id) {
        return TriggerKey.triggerKey(JOB_NAME + id);
    }

    /**
     * 新建定时任务
     *
     * @param spiderFlow 爬虫流程图
     * @return boolean true/false
     */
    public Date addJob(SpiderFlow spiderFlow) {
        try {
            JobDetail job = JobBuilder.newJob(SpiderJob.class).withIdentity(getJobKey(spiderFlow.getId())).build();
            job.getJobDataMap().put(JOB_PARAM_NAME, spiderFlow);
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(spiderFlow.getCron())
                                                                         .withMisfireHandlingInstructionFireAndProceed();
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(spiderFlow.getId()))
                                                .withSchedule(cronScheduleBuilder).build();
            return scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            LOGGER.error("创建定时任务出错", e);
            return null;
        }
    }

    public void run(String id) {
        Spider.getExecutorInstance().execute(() -> spiderJob.run(id));
    }

    public boolean remove(String id) {
        try {
            scheduler.deleteJob(getJobKey(id));
            return true;
        } catch (SchedulerException e) {
            LOGGER.error("删除定时任务失败", e);
            return false;
        }
    }
}
